package com.jjb.www.ui.dialog;

import android.content.Context;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.shape.view.ShapeImageView;
import com.jjb.www.R;
import com.jjb.www.aop.SingleClick;
import com.jjb.www.bean.PostBean;
import com.jjb.www.ui.adapter.ChoosePostAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: crg
 * time:   2021/9/14 11:02
 * des:    选择岗位类型
 */
public final class ChooseReasonDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder> implements BaseAdapter.OnItemClickListener {

        @Nullable
        private OnListener mListener;
        private ShapeImageView mImgCancel;
        private RecyclerView mRvChoosePost;
        private AppCompatTextView mTvTitle;



        private final ChoosePostAdapter adapter;
        /**
         * 岗位信息
         */
        private final List<PostBean> mList;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.choose_post_dialog);
            mList = new ArrayList<>();

            adapter = new ChoosePostAdapter(getContext());
            adapter.setOnItemClickListener(this);
            mImgCancel = findViewById(R.id.img_cancel);
            mTvTitle = findViewById(R.id.tv_title);
            mRvChoosePost = findViewById(R.id.rv_choose_post);
            mRvChoosePost.setLayoutManager(new LinearLayoutManager(getContext()));
            mRvChoosePost.setAdapter(adapter);
            setOnClickListener(mImgCancel);
        }

        public Builder setList(List<PostBean> list, Integer persion) {
            mList.addAll(list);
            adapter.setData(mList);
            adapter.setItemSel(persion);
            return this;
        }
        public Builder setTitle(String title) {
            mTvTitle.setText(title);
            return this;
        }
        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        @SingleClick
        @Override
        public void onClick(View view) {
            if (view == mImgCancel) {
                dismiss();
            }
        }

        @Override
        public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
            adapter.setItemSel(position);
            mListener.onCompleted(getDialog(),adapter.getItem(position),position);
            dismiss();
        }
    }

    public interface OnListener {

        /**
         * 选择完成时回调
         */
        void onCompleted(BaseDialog dialog, PostBean content,Integer position);

    }
}
